package com.zingrow.common.utils;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;

import freemarker.template.Configuration;
import freemarker.template.Template;

public class WordGenerator {
	private static Configuration configuration = null;
	private static Map<String,Template> allTemplates = null;
	private static Map<String,Template> allTemplatesword = null;
	
	static{
		configuration = new Configuration();
		configuration.setDefaultEncoding("utf-8");
		configuration.setClassForTemplateLoading(WordGenerator.class, "/com/zingrow/resume/");
		allTemplates = new HashMap<String, Template>();
		allTemplatesword = new HashMap<String, Template>();
		try {
			allTemplates.put("resume", configuration.getTemplate("resume.ftl"));
			allTemplatesword.put("resumeWord", configuration.getTemplate("resumeWord.ftl"));
		} catch (IOException e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}
	}
	private WordGenerator(){
		throw new AssertionError();
	}

	public static File createDoc(Map<String,Object> root,String type){
		String name = "temp"+ (int) (Math.random()*100000)+".docx";
		File f = new File(name);
		Template t = allTemplates.get(type);
		try {
			Writer w = new OutputStreamWriter(new FileOutputStream(f),"utf-8");
			t.process(root, w);
			w.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return f;
	}
	public static File createDocword(Map<String,Object> root,String type){
		String name = "temp"+ (int) (Math.random()*100000)+".docx";
		File f = new File(name);
		Template t = allTemplatesword.get(type);
		try {
			Writer w = new OutputStreamWriter(new FileOutputStream(f),"utf-8");
			t.process(root, w);
			w.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		return f;
	}
}
